package com.shengyuan.manage_os.old.ying.boce.jsy;

import java.io.File;

import com.shengyuan.manage_os.old.ying.boce.task.GenericTask;
import com.shengyuan.manage_os.old.ying.boce.task.TaskAdapter;
import com.shengyuan.manage_os.old.ying.boce.task.TaskListener;
import com.shengyuan.manage_os.old.ying.boce.task.TaskParams;
import com.shengyuan.manage_os.old.ying.boce.task.TaskResult;
import com.shengyuan.manage_os.old.ying.erpm.MainApplication;
import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.old.ying.erpm.WorkSpaceActivity;
import com.shengyuan.manage_os.old.ying.erpm.controls.CurrentTime;
import com.shengyuan.manage_os.old.ying.erpm.db.Actual_Data_Table;
import com.shengyuan.manage_os.old.ying.erpm.db.CLS_Actual_Data_Table;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;

@SuppressLint("HandlerLeak")
public class AdminPicHandlerTask {
	public static  AdminPicHandlerTask pMobileHandlerTask;
	public int  m_id, fujian_id , timeout = 60000;
	private CLS_Actual_Data_Table cls_Data;
	private boolean stop ;
	
	private ReNameTask mReNameTask; 
	private Handler uiHandler1 = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			mReNameTask = new ReNameTask();
			mReNameTask.setListener(mReNameTaskListener);
			mReNameTask.execute();
		}
	};
	
	public void start() {
		stop = false;
		uiHandler1.sendEmptyMessageDelayed(1, 2 * 1000);
	}
	
	public void stop(){
		stop = true;
	}


	private TaskListener mReNameTaskListener = new TaskAdapter() {
		private ProgressDialog	mDialog;
		@Override
		public void onPostExecute(GenericTask task, TaskResult result) {
			try {
				mDialog.dismiss();
				if (result == TaskResult.OK) {
					Utility.MessageBox("已经完成目录转换,目标目录:" + MainApplication.UploadPath);
				} else if (result == TaskResult.CANCELLED) {
					Utility.MessageBox(MainApplication.Instance.getString(R.string.admin_status_invalid_username_or_password));
				}
			} catch (Exception e) {
			}

		}
		
		@Override
		public void onProgressUpdate(GenericTask task, Object param) {
			try {
				mDialog.setMessage("目录ID:" + param.toString());
			} catch (Exception e) {
				// TODO: handle exception
			}
			
		}

		@Override
		public void onPreExecute(GenericTask task) {
			mDialog = Utility.ShowModelDialog(WorkSpaceActivity.Instance, 
					MainApplication.Instance.getString(R.string.admin_status_logging_in));
			mDialog.show();
		}
	};
	private class ReNameTask extends GenericTask
	{
		@Override
		protected TaskResult _doInBackground(TaskParams... params) {
			SendPicFuJianToUpLoadDir();
			return TaskResult.OK;
		}
	
		private void SendPicFuJianToUpLoadDir() {
			fujian_id =1;
			Actual_Data_Table dbo = new Actual_Data_Table(MainApplication.Instance);
			try {
				while (fujian_id >0) {
					
					cls_Data = dbo.get_Sended_all( "1" );
					fujian_id = cls_Data.Id;
					if (fujian_id >0) {
						publishProgress(fujian_id);
							//建立 uploadfile/yearmoth/idtime/json1
						String subpath = MainApplication.UploadPath + 
								(1900+CurrentTime.getTime().getYear()) 
								+ (CurrentTime.getTime().getMonth()+1) +"/";
						File subFile = new File(subpath);
						if (!subFile.exists() && !subFile.isDirectory()) {
							if (!subFile.mkdirs()){}
						}
						File source = new File(MainApplication.filePath + cls_Data.Id);
						File dest = new File(subpath + cls_Data.OVERTIME);
						if(source.exists() && source.renameTo(dest))
						{
							source.delete();
						}
						dbo.Update_Data(fujian_id, dbo.SERVICEID, "2");
					}
				}			
			} catch (Exception e) {
				Utility.MessageBox(e.getMessage());
			}
	
			dbo.close();
		}
	}	

}
